﻿using QYmall.Core.Common.Emum;
using QYmall.Core.Common.Extensions;
using QYmall.Core.Common.Helper;
using QYmall.Core.DBFactory.Database;
using QYmall.Core.IServices;
using QYmall.Core.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace QYmall.Core.Services
{
    /// <summary>
    /// 快递公司接口实现
    /// </summary>
    public class ShopExpressService : BaseService, IShopExpressService
    {
        public ShopExpressService(IDbContextFactory contentFactory) : base(contentFactory) { }

        /// <summary>
        /// 根据条件删除记录
        /// </summary>
        public async Task<bool> DeleteAsync(Expression<Func<ShopExpress, bool>> funcWhere, WriteRoRead writeAndRead = WriteRoRead.Write)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库

            //查找主表
            var list = await _context.Set<ShopExpress>().Where(funcWhere).ToListAsync();
            if (list == null) return false;
            var listIds = list.Select(x => x.Id).ToList();
            //查找关系表
            var sList = await _context.Set<OrderDelivery>().Where(x => listIds.Contains(x.ExpressId)).ToListAsync();
            //删除主从表
            _context.Set<OrderDelivery>().RemoveRange(sList);
            _context.Set<ShopExpress>().RemoveRange(list);
            //提交执行删除
            return await this.SaveAsync();
        }
    }
}
